import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_music/page/controller/main_controller.dart';
import 'package:flutter_music/res/colors.dart';
import 'package:flutter_music/widgets/base_statefull_widget.dart';
import 'package:flutter_music/widgets/keep_alive_wrapper.dart';
import 'package:get/get.dart';

class MainPage extends BaseStatefulWidget<MainController> {
  const MainPage({Key? key}) : super(key: key);


  @override
  Widget buildContent(BuildContext context) {
    // TODO: implement buildContent
    return Obx(() => WillPopScope(
        child: Scaffold(
          body: KeepAliveWrapper(
            child: PageView(
              controller: controller.pageController,
              physics: const NeverScrollableScrollPhysics(),
              children: controller.naviItems,
            ),
          ),
          bottomNavigationBar: BottomNavigationBar(
            elevation: 8.0,
              type: BottomNavigationBarType.fixed,
              currentIndex: controller.curPage.value,
              items: const [
                BottomNavigationBarItem(icon: Icon(Icons.home),label: "首页"),
                BottomNavigationBarItem(icon: Icon(Icons.explore),label: "发现"),
                BottomNavigationBarItem(icon: Icon(Icons.local_fire_department),label: "热门"),
                BottomNavigationBarItem(icon: Icon(Icons.person),label: "我的"),
              ],
            fixedColor: ColorStyle.color_EA4C43,
            onTap: (index) {
                controller.pageController.jumpToPage(index);
                controller.curPage.value = index;
            },
          ),
        ),
        onWillPop: () async {
          if(controller.lastPopTime == null || DateTime.now().difference(controller.lastPopTime!)
          > const Duration(seconds: 2)) {
            controller.lastPopTime = DateTime.now();
            showToast("再按一次退出app");
            return false;
          }else {
            controller.lastPopTime = DateTime.now();
            await SystemNavigator.pop();
            return true;
          }
        }));
  }

  @override
  bool showTitleBar() {
    // TODO: implement showTitleBar
    return false;
  }

}